package com.nmh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nmh.config.R;
import com.nmh.entity.DictType;
import com.nmh.mapper.dictTypeMapper;
import com.nmh.service.dictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/dict")
@Api(tags="数据字典模块")
public class dictTypeController {
    @Autowired
    private dictTypeService dictTypeService;
    @Autowired
    private dictTypeMapper dictTypeMapper;
    /*
    牲畜品种添加接口
     */
    @ApiOperation(value="牲畜品种添加接口")
    @PostMapping()
    public R<String> save(@RequestBody DictType dictType){
        dictTypeService.save(dictType);
        return R.success("添加成功");
    }
    /*
    展示左边导航栏接口
     */
    @ApiOperation(value="展示左边导航栏接口")
    @GetMapping
    public R<List<DictType>> get(){
        List<DictType> list=dictTypeMapper.getDictTypes();
        List<String> list1=new ArrayList<>();
        for(DictType dict:list){
            list1.add(dict.getDictType());
        }
        return R.success(list);
    }
    /*
    点击对应的获取相应的数据
     */
    @ApiOperation(value="点击对应的获取相应的数据")
    @GetMapping("/getName")
    public R<Page<DictType>> getName(String dictType,int pageNumber,int pageSize){
        Page<DictType> pageInfo=new Page<>(pageNumber,pageSize);
        LambdaQueryWrapper<DictType> wrapper =new LambdaQueryWrapper<DictType>()
                .eq(DictType::getDictType,dictType);
        dictTypeService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }
    /*
    在相应类型中添加
     */
    @ApiOperation(value="在相应类型中添加")
    @PostMapping("/save")
    public R<String> saves(@RequestBody DictType dicts){
        LambdaQueryWrapper<DictType> wrapper=new LambdaQueryWrapper<DictType>()
                .eq(DictType::getDictType,dicts.getDictType()).eq(DictType::getDictName,dicts.getDictName());
        DictType di=dictTypeService.getOne(wrapper);
        if(di==null){
            Integer id=dictTypeMapper.getByone(dicts.getDictType());
            DictType dict=new DictType();
            dict.setDictId(id);
            dict.setDictType(dicts.getDictType());
            dict.setDictName(dicts.getDictName());
            dictTypeService.save(dict);
            return R.success("添加成功");
        }
        return R.error("该"+dicts.getDictName()+"已经存在","401");
    }
    /*
   状态修改
     */
    @ApiOperation(value="状态修改")
    @PutMapping
    public R<String> put(@RequestBody DictType dict){
        LambdaQueryWrapper<DictType> wrapper=new LambdaQueryWrapper<DictType>()
                .eq(DictType::getDictId,dict.getDictId()).eq(DictType::getDictName,dict.getDictName());
        DictType dicts=dictTypeService.getOne(wrapper);
        if(dicts.getStatus().equals("1")){
            dicts.setStatus("0");
        }else{
            dicts.setStatus("1");
        }
        dictTypeService.update(dicts,wrapper);
        return R.success("修改成功");
    }
    /*
    模糊查询
     */
    @ApiOperation(value="模糊查询")
    @GetMapping("/get")
    public R<Page<DictType>> gets(int pageNumber,int pageSize,String dictStatus,String dictName,String dictType){
        Page<DictType> pageInfo=new Page<>(pageNumber,pageSize);
        if(dictStatus!=null){
            if(dictStatus.equals("\"\"")){
                LambdaQueryWrapper<DictType> wrapper=new LambdaQueryWrapper<DictType>()
                        .like(StringUtils.isNotEmpty(dictName),DictType::getDictName,dictName)
//                    .like(StringUtils.isNotEmpty(dict.getStatus()),DictType::getStatus,dict.getStatus())
                        .eq(DictType::getDictType,dictType);
                dictTypeService.page(pageInfo,wrapper);
                return R.success(pageInfo);
            }
        }
        LambdaQueryWrapper<DictType> wrapper=new LambdaQueryWrapper<DictType>()
                .like(StringUtils.isNotEmpty(dictName),DictType::getDictName,dictName)
                .like(StringUtils.isNotEmpty(dictStatus),DictType::getStatus,dictStatus)
                .eq(DictType::getDictType,dictType);
        dictTypeService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }
    /*
    获取牲畜品种
     */
    @ApiOperation(value="获取牲畜品种")
    @GetMapping("/getSort")
    public List<String> getSort(){
        LambdaQueryWrapper<DictType> wrapper=new LambdaQueryWrapper<DictType>()
                .eq(DictType::getDictType,"牲畜品种");
        List<DictType> list=dictTypeService.list(wrapper);
        List<String> list1=new ArrayList<>();
        for(DictType d:list){
            list1.add(d.getDictName());
        }
        return list1;
    }
}
